package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Equipment;
import com.example.springboot.entity.RepairForm;
import com.example.springboot.entity.User;
import com.example.springboot.service.RepairService;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 功能：
 * 作者：zhoujun
 * 日期： 2024/5/3 18:06
 */

@RestController
@RequestMapping("/repair")
public class RepairFormController {
    @Autowired
    RepairService repairService;
    @Autowired
    UserService userService;
    @HoneyLogs(operation = "报修", type = LogType.ADD)
    @PostMapping("/add")
    public Result add(@RequestBody RepairForm repairForm){
        User currentUser = TokenUtils.getCurrentUser();  // 获取到当前登录的用户信息
        repairForm.setStatus("未处理");

        repairForm.setTime(DateUtil.now());
        repairForm.setName(currentUser.getUsername());
       repairService.save(repairForm);
        return Result.success();
    }

    @HoneyLogs(operation = "报修", type = LogType.UPDATE)
    @PutMapping("/update")
    public Result update(@RequestBody RepairForm repairForm){

        repairService.updateById(repairForm);
        return Result.success();
    }

    @HoneyLogs(operation = "报修", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        repairService.removeById(id);
        return Result.success();
    }

    @HoneyLogs(operation = "报修", type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        repairService.removeBatchByIds(ids);
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result SelectAll(){
        List<RepairForm> repairFormsList= repairService.list(new QueryWrapper<RepairForm>().orderByDesc("id"));
        return Result.success(repairFormsList);
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        RepairForm repairForm = repairService.getById(id);
        return Result.success(repairForm);
    }


    /** 根据手机号查询
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String phonenum) {
        QueryWrapper<RepairForm> queryWrapper = new QueryWrapper<RepairForm>();
        queryWrapper.like(StrUtil.isNotBlank(phonenum),"phonenum",phonenum);

        Page<RepairForm> page = repairService.page(new Page<>(pageNum, pageSize),queryWrapper);

        return Result.success(page);
    }


    /** 根据状态为未处理查询
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPageStatus")
    public Result selectByPageStatus(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String status) {
        QueryWrapper<RepairForm> queryWrapper = new QueryWrapper<RepairForm>();
        if ("未处理".equals(status)) {
            queryWrapper.eq("status", status);
        }

        Page<RepairForm> page = repairService.page(new Page<>(pageNum, pageSize),queryWrapper);

        return Result.success(page);
    }


    /** 根据状态为处理中查询
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPageStatusHandling")
    public Result selectByPageStatusHandling(@RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize,
                                     @RequestParam String status) {
        QueryWrapper<RepairForm> queryWrapper = new QueryWrapper<RepairForm>();
        if ("处理中".equals(status)) {
            queryWrapper.eq("status", status);
        }

        Page<RepairForm> page = repairService.page(new Page<>(pageNum, pageSize),queryWrapper);

        return Result.success(page);
    }


    /** 根据状态为已完成查询
     * 多条件模糊查询用户信息
     * pageNum 当前的页码1
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPageStatusHandled")
    public Result selectByPageStatusHandled(@RequestParam Integer pageNum,
                                             @RequestParam Integer pageSize,
                                             @RequestParam String status) {
        QueryWrapper<RepairForm> queryWrapper = new QueryWrapper<RepairForm>();
        if ("已完成".equals(status)) {
            queryWrapper.eq("status", status);
        }

        Page<RepairForm> page = repairService.page(new Page<>(pageNum, pageSize),queryWrapper);

        return Result.success(page);
    }






    @GetMapping("/selectByPages")
    public Result selectByPages(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(value = "phonenum", required = false) String phonenum) {
        // 获取当前登录用户的ID和手机号
        User currentUser = TokenUtils.getCurrentUser();
        String currentUserPhone = currentUser.getPhone();

        // 创建查询条件
        QueryWrapper<RepairForm> queryWrapper = new QueryWrapper<>();
        // 确保只查询当前用户的记录
        queryWrapper.eq("phonenum", currentUserPhone); // 假设 RepairForm 表中有一个 user_phone 字段用于存储用户手机号

        // 如果提供了手机号参数，并且手机号不为空或空白，则添加手机号查询条件
        if (StrUtil.isNotBlank(phonenum)) {
            queryWrapper.and(wrapper -> wrapper.like("phonenum", phonenum));
        }

        // 分页查询
        IPage<RepairForm> page = repairService.page(new Page<>(pageNum, pageSize), queryWrapper);

        // 返回分页结果
        return Result.success(page);
    }

}